using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;

namespace IDE_GUI
{
    public partial class frmIdeDocument : DockContent 
    {
        protected string fileName = null;

        public frmIdeDocument()
        {
            InitializeComponent();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.fileName == null)
            {
                SaveAs();
            }
            else
            {
                SaveFile(fileName);
            }
        }
        
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFile();
        }

        public void OpenFile()
        {
            if (open.ShowDialog(this) == DialogResult.OK)
            {
                OpenFile(open.FileName);
            }
        }

        public void OpenFile(string filename)
        {
            if (File.Exists(filename))
            {
                TextReader reader = File.OpenText(filename);

                edit.ClearAll();
                edit.Text = reader.ReadToEnd();

                reader.Close();

                this.fileName = filename;
            }
            else
            {
                throw new ArgumentException("File not found.", "filename");
            }
        }

        public void SaveAs()
        {
            if (save.ShowDialog(this.MdiParent) == DialogResult.OK)
            {
                SaveFile(save.FileName);
                this.fileName = save.FileName;
            }
        }

        public void SaveFile(string filename)
        {
            TextWriter writer = File.CreateText(filename);

            writer.Write(edit.Text);

            writer.Flush();
            writer.Close();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAs();
        }
    }
}